<?php
//计算数据库大小
function sizecount($size) 
{
	if($size >= 1073741824) {
		$size = round($size / 1073741824 * 100) / 100 . ' GB';
	} elseif($size >= 1048576) {
		$size = round($size / 1048576 * 100) / 100 . ' MB';
	} elseif($size >= 1024) {
		$size = round($size / 1024 * 100) / 100 . ' KB';
	} else {
		$size = $size . ' Bytes';
	}
	return $size;
}
//HTML转文本
function SpHtml2Text($str)
{
	$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
	$alltext = "";
	$start = 1;
	for($i=0;$i<strlen($str);$i++)
	{
		if($start==0 && $str[$i]==">")
		{
			$start = 1;
		}
		else if($start==1)
		{
			if($str[$i]=="<")
			{
				$start = 0;
				$alltext .= "";
			}
			else if(ord($str[$i])>31)
			{
				$alltext .= $str[$i];
			}
		}
	}
	$alltext = str_replace("　","",$alltext);
	$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
	$alltext = preg_replace("/[ ]+/s"," ",$alltext);
	return $alltext;
}

function cn_substr_utf8($str, $length, $start=0)
{
	if(strlen($str) < $start+1)
    {
    	return '';
    }
    preg_match_all("/./su", $str, $ar);
    $str = '';
    $tstr = '';
	//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
    for($i=0; isset($ar[0][$i]); $i++)
    {
    	if(strlen($tstr) < $start)
        {
        	$tstr .= $ar[0][$i];
        }
        else
        {
        	if(strlen($str) < $length + strlen($ar[0][$i]) )
            {
            	$str .= $ar[0][$i];
            }
            else
            {
            	break;
            }
        }
	}
	return $str;
}
//读取分类缓存
function getCategoryCache()
{
	if (!F('categoryCache'))
	{
		$categoryCache = M('category')->select();
		F('categoryCache',$categoryCache);
	}
	return F('categoryCache');	
}

function getArticleCategoryName($id)
{
	return M('category')->where(array('id'=>$id))->getField('c_name');
}

/**
 * 寻找两数组所有不同元素
 *
 * @param array $array1
 * @param array $array2
 * @return array
 */
function findArray($array1,$array2){
    $r1 = array_diff($array1, $array2);
    $r2 = array_diff($array2, $array1);
    $r = array_merge($r1, $r2);
    return $r;
}

//取第一个图片为缩略图
function getThumbFromBody($body){
	$litpic = '';
    preg_match_all("/(src)=[\"|'| ]{0,}([^>]*\.(gif|jpg|bmp|png))/isU",$body,$img_array);
    $img_array = array_unique($img_array[2]);
    if(count($img_array)>0)
    {
        $picname = preg_replace("/[\"|'| ]{1,}/", '', $img_array[0]);
        if(preg_match("#_lit\.#", $picname)) $litpic = $picname;
        else $litpic = '';
    }
    return $litpic;
}

function getGravatar($email, $s=36, $d='mm', $g='g') {
	$hash = md5($email);
	$avatar = "http://www.gravatar.com/avatar/$hash?s=$s&d=$d&r=$g";
	return $avatar;
}
?>